package com.example.administrator.myapplication;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;

public class MerchandiseAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater layoutInflater;
    private onMyAdapterClicklistener clicklistener;
    public ArrayList<SkuList> data=new ArrayList<>();
    public MerchandiseAdapter(Context context) {
        this.mContext=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setData(ArrayList<SkuList> lists){
       this.data.clear();
       this.data.addAll(lists);
       notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final viewHolder viewHolder;

        if (convertView==null){
            viewHolder=new viewHolder();
            convertView=layoutInflater.inflate(R.layout.activity_everymerchandise,null);
            viewHolder.skuName=convertView.findViewById(R.id.tast_item);
            viewHolder.cupType=convertView.findViewById(R.id.cup_type);
            viewHolder.price=convertView.findViewById(R.id.price_item);
            viewHolder.deleteButton=convertView.findViewById(R.id.delete_button);
            viewHolder.reviseButton=convertView.findViewById(R.id.revise_button);
            viewHolder.mSwitch=convertView.findViewById(R.id.sw1);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder= (MerchandiseAdapter.viewHolder) convertView.getTag();
        }
            DecimalFormat df=new DecimalFormat("0.00");
            viewHolder.skuName.setText(data.get(position).sku_name);
            viewHolder.cupType.setText(data.get(position).cup_type);
            viewHolder.price.setText("¥"+df.format(data.get(position).price));
            viewHolder.deleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clicklistener.onDeleteClick(position);
                }
            });
            viewHolder.reviseButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clicklistener.onReviseClick(position);

                }
            });
           viewHolder.mSwitch.setOnCheckedChangeListener(null);
        if (data.get(position).getOn_sale()==1){
            viewHolder.mSwitch.setChecked(true);
        }else {
            viewHolder.mSwitch.setChecked(false);
        }


        viewHolder.mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    clicklistener.onSwitchClick(position,isChecked);
                }
            });

            return convertView;
    }
    public class viewHolder{
        TextView skuName,price,cupType;
        Button deleteButton,reviseButton;
        Switch mSwitch;
    }
    public interface onMyAdapterClicklistener{
        void onDeleteClick(int position);
        void onReviseClick(int position);
        void onSwitchClick(int position,boolean isChecked);

}
public void setListener(onMyAdapterClicklistener clicklistener){
        this.clicklistener=clicklistener;


}
}


